package com.example.demodb.controller;

import com.example.demodb.entity.Song;
import com.example.demodb.entity.User;
import com.example.demodb.entity.UserLove;
import com.example.demodb.entity.UserSong;
import com.example.demodb.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/UserController/findAll")
    @CrossOrigin
    public List<User> findAll(){
        return userService.findAll();
    }

    @PostMapping("/UserController/login") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public String login(@RequestParam("username")String username, @RequestParam("password")String password){
        return userService.login(username, password);//返回为空的话,就说明为0
    }

    @GetMapping("/UserController/deleteById/{id}")
    @CrossOrigin
    public void delete(@PathVariable String id){
        userService.delete(id);
    }

    @GetMapping("/UserController/findById/{id}")
    @CrossOrigin
    public User findById(@PathVariable String id){
        return userService.findById(id);
    }

    @GetMapping("/UserController/findUserSongsByUserId/{userId}")
    @CrossOrigin
    public Set<UserSong> findUserSongsByUserId(@PathVariable String userId){
        return userService.findUserSongsByUserId(userId);
    }

    @GetMapping("/UserController/findAllUserSongsByUserOrderByUpdateTimeDesc/{userId}")
    @CrossOrigin
    public List<UserSong> findAllUserSongsByUserOrderByUpdateTimeDesc(@PathVariable String userId){
        return userService.findAllUserSongsByUserOrderByUpdateTimeDesc(userId);
    }

    @GetMapping("/UserController/findAllUserSongsByUserOrderByUpdateTime/{userId}")
    @CrossOrigin
    public List<UserSong> findAllUserSongsByUserOrderByUpdateTime(@PathVariable String userId){
        return userService.findAllUserSongsByUserOrderByUpdateTime(userId);
    }

    @PostMapping("/UserController/add") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public String add(@RequestBody User user){
        return userService.add(user);//返回为-1的话,就说明为0
    }


    @GetMapping("/UserController/addUserSong") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public UserSong addUserSong(@RequestParam("userId") String userId, @RequestParam("songId") String songId, @RequestParam(value = "listenCount" ,required = false) Integer listenCount){
        return userService.addUserSongLimitByUserSongNum(userId, songId, listenCount);//返回为空的话,就说明为0
    }

    @GetMapping("/UserController/addUserLove/{userId}/{songId}") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public UserLove addUserLove(@PathVariable String userId, @PathVariable String songId){
        return userService.addUserLove(userId, songId);//返回为空的话,就说明为0
    }

    @GetMapping("/UserController/deleteUserLove/{userId}/{songId}") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public String deleteUserLove(@PathVariable String userId, @PathVariable String songId){
        return userService.deleteUserLove(userId, songId);//返回为空的话,就说明为0
    }


//    findUserSongTitleByUserId
    @GetMapping("/UserController/findUserSongTitleByUserId/{userId}")
    @CrossOrigin
    public List<String> findUserSongTitleByUserId(@PathVariable String userId){
        return userService.findUserSongTitleByUserId(userId);
    }

    @PostMapping("/UserController/update") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public void update(@RequestBody User user){
        userService.update(user);//返回为空的话,就说明为0
    }

    @GetMapping("/UserController/updateUserNameById") //RO表示该接口 会返回一个对象
    @CrossOrigin
    public User updateUserNameById(@RequestParam("id") String id, @RequestParam("name") String name){
        return userService.updateUserName(id,name);//返回为空的话,就说明为0
    }

}
